package data;

import com.itany.ums.constant.RoleEnum;
import com.itany.ums.constant.StateEnum;
import com.itany.ums.entity.User;

// 数据存储设备
// 随着存储技术的改变, 会从程序中分离出去
// 是独立于程序的存在
public class Datasource {

	// 该变量的作用: 当对用户进行保存时, 让该变量自增以产生一个新的编号
	public static long id = 1;

	public static User[] users;

	static {
		users = new User[0];
		// 给数据库中创建一些模拟数据
		User u1 = new User(id++, "alice", "123456", "13000000001", "alice@user.com", RoleEnum.ADMIN.getRoleCode(), StateEnum.ENABLE.getStateCode());
		saveUser(u1);

		User u2 = new User(id++, "tom", "123456", "13000000002", "tom@user.com", RoleEnum.NORMAL.getRoleCode(), StateEnum.ENABLE.getStateCode());
		saveUser(u2);

		User u3 = new User(id++, "monica", "123456", "13000000003", "monica@user.com", RoleEnum.NORMAL.getRoleCode(), StateEnum.ENABLE.getStateCode());
		saveUser(u3);

		User u4 = new User(id++, "angelina", "123456", "13000000001", "alice@user.com", RoleEnum.NORMAL.getRoleCode(), StateEnum.DISABLE.getStateCode());
		saveUser(u4);
	}

	public static void saveUser(User u) {
		User[] tmp = new User[users.length + 1];
		for (int i = 0; i < users.length; i++) {
			tmp[i] = users[i];
		}
		tmp[users.length] = u;
		users = tmp;
	}

}
